package com.shop.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.stream.StreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.shop.dto.*;
import com.shop.entity.CartEntity;
import com.shop.local.UserInfoLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wzby
 * @Date: 2022/10/21 9:58
 */
@Service
public class CartService {

    @Resource(name = "redisTemplate")
    private HashOperations<String, String, CartEntity> hashOperations;
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Long> valueOperations;
    @Autowired
    private ProductService productService;

    private static String cartId = "cartId";

    public Integer addCart(CartDto cartDto) {
        UserInfo userInfo = UserInfoLocal.get();
        String cartKey = "cart." + userInfo.getMemberId();
        String productId = String.valueOf(cartDto.getProductId());
        Boolean key = hashOperations.hasKey(cartKey, productId);
        if (key) {
            CartEntity currCart = hashOperations.get(cartKey, productId);
            currCart.setQty(currCart.getQty() + cartDto.getQty());
            hashOperations.put(cartKey, productId, currCart);
        } else {
            CartEntity cartEntity = BeanUtil.copyProperties(cartDto, CartEntity.class);
            Long id = valueOperations.increment(cartId);
            cartEntity.setId(id);
            ProductDtoQuery query = new ProductDtoQuery();
            query.setId(cartDto.getProductId());
            List<ProductDto> products = productService.select(query);
            cartEntity.setPrice(products.get(0).getPrice());
            //获取你所获得商品id的价格

            hashOperations.put(cartKey, productId, cartEntity);
        }
        return 1;
    }

    public List<CartDto> selectCart(CartQueryDto cartQueryDto) {
        List<CartEntity> cartEntities = new ArrayList<>();
        UserInfo userInfo = UserInfoLocal.get();
        String cartKey = "cart." + userInfo.getMemberId();
        Integer[] productIds = cartQueryDto.getProductIds();

        if (ObjectUtil.isNotEmpty(productIds)) {
            List<String> pIds = StreamUtil.of(productIds).map(item -> item.toString()).collect(Collectors.toList());

            List<CartEntity> cartEntities2 = hashOperations.multiGet(cartKey, pIds);
            List<CartEntity> cartEntityList = cartEntities;
            cartEntities2.stream().forEach(item -> {
                if (item.getId() != null) {
                    cartEntityList.add(item);
                }
            });
        } else {

            cartEntities = hashOperations.values(cartKey);
        }
        List<CartDto> cartDtos = BeanUtil.copyToList(cartEntities, CartDto.class);
        return cartDtos;
    }

    //删除单个商品
    public Long deleteCart(Integer memberId, Integer productId) {
        String cartKey = "cart." + memberId;
        return hashOperations.delete(cartKey, productId.toString());
    }

    //清空购物车或清除部分商品
    public void clearCart(Integer memberId, List<Integer> productIds) {
        String cartKey = "cart." + memberId;
        List<String> productListStr = productIds.stream().map(item -> item.toString()).collect(Collectors.toList());
        for(String productIdKey:productListStr){
            hashOperations.delete(cartKey,productIdKey);
        }
    }

    public Integer updateQtyCart(CartQtyUpdate cartQtyUpdate) {
        String cartKey = "cart." + cartQtyUpdate.getMemberId();
        String productId = cartQtyUpdate.getProductId().toString();

        CartEntity cartEntity = hashOperations.get(cartKey, productId);
        cartEntity.setQty(cartQtyUpdate.getQty());

        hashOperations.put(cartKey, productId, cartEntity);

        return 1;
    }
}
